| Conditions | 2 |
| Paths | 8 |
| Total Lines | 103 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | ////////////////////////////////////////////////////////////////////////////////////// |
||
| 187 | function draw(str, to, first, box) { |
||
| 188 | tag || init(); |
||
| 189 | |||
| 190 | var re = /("(?:((?:https?|file):\/\/(?:\\?\S)+?)|(?:\\?.)*?)")\s*(:?)|-?\d+\.?\d*(?:e[+-]?\d+)?|true|false|null|[[\]{},]|(\S[^-[\]{},"\d]*)/gi, |
||
| 191 | node = div.cloneNode(), |
||
| 192 | link = document.createElement("a"), |
||
| 193 | span = document.createElement("span"), |
||
| 194 | info = document.createElement("i"), |
||
| 195 | colon = document.createTextNode(": "), |
||
| 196 | comma = fragment(","), |
||
| 197 | path = [], |
||
| 198 | cache = { |
||
| 199 | "{": fragment("{", "}"), |
||
| 200 | "[": fragment("[", "]") |
||
| 201 | }; |
||
| 202 | |||
| 203 | node.className = "R" + rand + (box ? " " + box : ""); |
||
| 204 | |||
| 205 | link.classList.add("L" + rand); |
||
| 206 | info.classList.add("I" + rand); |
||
| 207 | |||
| 208 | to.addEventListener("click", function(e) { |
||
| 209 | var target = e.target, |
||
| 210 | open = target.classList.contains(COLL); |
||
| 211 | if (target.tagName == "I") { |
||
| 212 | if (e.altKey) { |
||
| 213 | changeSiblings(target, COLL, !open); |
||
| 214 | } else if (e[mod]) { |
||
| 215 | open = target.nextSibling.querySelector("i"); |
||
| 216 | if (open) change(target.nextSibling, "i", COLL, !open.classList.contains(COLL)); |
||
| 217 | } else { |
||
| 218 | target.classList[open ? "remove" : "add"](COLL); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | }, true); |
||
| 222 | |||
| 223 | to.replaceChild(box = node, first); |
||
| 224 | loop(str, re); |
||
| 225 | |||
| 226 | function loop(str, re) { |
||
| 227 | str = reconvert(str); |
||
| 228 | var match, val, tmp, i = 0, |
||
| 229 | len = str.length; |
||
| 230 | try { |
||
| 231 | for (; match = re.exec(str);) { |
||
| 232 | val = match[0]; |
||
| 233 | if (val == "{" || val == "[") { |
||
| 234 | path.push(node); |
||
| 235 | node.appendChild(cache[val].cloneNode(true)); |
||
| 236 | node = node.lastChild.previousSibling; |
||
| 237 | node.len = 1; |
||
| 238 | node.start = re.lastIndex; |
||
| 239 | } else if ((val == "}" || val == "]") && node.len) { |
||
| 240 | if (node.childNodes.length) { |
||
| 241 | tmp = info.cloneNode(); |
||
| 242 | tmp.dataset.content = node.len + ( |
||
| 243 | node.len == 1 ? |
||
| 244 | (val == "]" ? " item, " : " property, ") : |
||
| 245 | (val == "]" ? " items, " : " properties, ") |
||
| 246 | ) + units(re.lastIndex - node.start + 1); |
||
| 247 | |||
| 248 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 249 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, "\\'")); |
||
| 250 | } |
||
| 251 | node.parentNode.insertBefore(tmp, node); |
||
| 252 | } else { |
||
| 253 | node.parentNode.removeChild(node); |
||
| 254 | } |
||
| 255 | node = path.pop(); |
||
| 256 | } else if (val == ",") { |
||
| 257 | node.len += 1; |
||
| 258 | node.appendChild(comma.cloneNode(true)); |
||
| 259 | } else { |
||
| 260 | if (match[2]) { |
||
| 261 | tmp = link.cloneNode(); |
||
| 262 | tmp.href = match[2].replace(/\\"/g, '"'); |
||
| 263 | } else { |
||
| 264 | tmp = span.cloneNode(); |
||
| 265 | } |
||
| 266 | tmp.textContent = match[1] || val; |
||
| 267 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 268 | node.appendChild(tmp); |
||
| 269 | if (match[3]) { |
||
| 270 | node.appendChild(colon.cloneNode()); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | if (++i > 1000) { |
||
| 274 | document.title = (0 | (100 * re.lastIndex / len)) + "% of " + units(len); |
||
| 275 | return setTimeout(function() { |
||
| 276 | loop(str, re) |
||
| 277 | }); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | document.title = "" |
||
| 281 | JSON.parse(str) |
||
| 282 | } catch (e) { |
||
| 283 | tmp = document.createElement("h3"); |
||
| 284 | tmp.className = ERR; |
||
| 285 | tmp.textContent = e; |
||
| 286 | box.insertBefore(tmp, box.firstChild); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 336 |